package com.atguigu.tingshu.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 订单修改完成后,通知其他服务同步购买数据
 */
@Configuration
public class OrderPaidRabbitConfig {

    /**
     * 广播交换机
     * @return
     */
    @Bean("orderPaidFanoutExchange")
    public Exchange orderPaidFanoutExchange(){
        return ExchangeBuilder.fanoutExchange("order_paid_fanout_exchange").build();
    }

    /**
     * 专辑服务监听队列
     * @return
     */
    @Bean("orderPaidAlbumQueue")
    public Queue orderPaidAlbumQueue(){
        return QueueBuilder.durable("order_paid_album_queue").build();
    }

    /**
     * 专辑绑定
     * @param orderPaidFanoutExchange
     * @param orderPaidAlbumQueue
     * @return
     */
    @Bean
    public Binding orderPaidAlbumQueueBinding(@Qualifier("orderPaidFanoutExchange") Exchange orderPaidFanoutExchange,
                                              @Qualifier("orderPaidAlbumQueue") Queue orderPaidAlbumQueue){
        return BindingBuilder.bind(orderPaidAlbumQueue).to(orderPaidFanoutExchange).with("").noargs();
    }

    /**
     * 搜索服务监听队列
     * @return
     */
    @Bean("orderPaidSearchQueue")
    public Queue orderPaidSearchQueue(){
        return QueueBuilder.durable("order_paid_search_queue").build();
    }

    /**
     * 搜索绑定
     * @param orderPaidFanoutExchange
     * @param orderPaidSearchQueue
     * @return
     */
    @Bean
    public Binding orderPaidSearchQueueBinding(@Qualifier("orderPaidFanoutExchange") Exchange orderPaidFanoutExchange,
                                              @Qualifier("orderPaidSearchQueue") Queue orderPaidSearchQueue){
        return BindingBuilder.bind(orderPaidSearchQueue).to(orderPaidFanoutExchange).with("").noargs();
    }

    /**
     * 用户服务监听队列
     * @return
     */
    @Bean("orderPaidUserQueue")
    public Queue orderPaidUserQueue(){
        return QueueBuilder.durable("order_paid_user_queue").build();
    }

    /**
     * 用户绑定
     * @param orderPaidFanoutExchange
     * @param orderPaidUserQueue
     * @return
     */
    @Bean
    public Binding orderPaidUserQueueBinding(@Qualifier("orderPaidFanoutExchange") Exchange orderPaidFanoutExchange,
                                               @Qualifier("orderPaidUserQueue") Queue orderPaidUserQueue){
        return BindingBuilder.bind(orderPaidUserQueue).to(orderPaidFanoutExchange).with("").noargs();
    }

    /**
     * 账户服务监听队列
     * @return
     */
    @Bean("orderPaidAccountQueue")
    public Queue orderPaidAccountQueue(){
        return QueueBuilder.durable("order_paid_account_queue").build();
    }

    /**
     * 账户绑定
     * @param orderPaidFanoutExchange
     * @param orderPaidAccountQueue
     * @return
     */
    @Bean
    public Binding orderPaidAccountQueueBinding(@Qualifier("orderPaidFanoutExchange") Exchange orderPaidFanoutExchange,
                                             @Qualifier("orderPaidAccountQueue") Queue orderPaidAccountQueue){
        return BindingBuilder.bind(orderPaidAccountQueue).to(orderPaidFanoutExchange).with("").noargs();
    }
}
